/**
 * problem_012.c
 * Copyright (C) 2011-03-18 - xrose
 * What is the value of the first triangle number to have over five hundred divisors?
 */
#include <stdio.h>
#include <math.h>
#define triangle(x) (x)*(x+1)/2
int isTriangle(int numb);
int hmany_divisor(int numb);
int main (int argc, char *argv[])
{
    register int i=1;
    while(hmany_divisor(triangle(i))<=500)
    {
        i++;
        //printf("__i__= %d\n", i);
    }
//    printf("hmany__:%d\n", hmany_divisor(i));
    printf("Answer is: %d\n", i*(i+1)/2);
    return 0;
}

int isTriangle(int numb)
{
    int n = floor(sqrt(numb));
    if(n*(n+1) == 2*numb)
    {
        return 1;
    } else
    {
        return 0;
    }
}
int hmany_divisor(int numb)
{
    register int i;
    register int count =0;
    int num = floor(sqrt(numb));
    for(i=1; i<=num; i++)
    {
        if (numb%i == 0)
        {
            count +=1;
        }
    }
    if(numb == num*num)
    {
        return 2*count -1;
    } else
    {
        return 2*count;
    }
}
